

package com.loong.loong.hik.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loong.loong.common.core.util.R;
import com.loong.loong.common.excel.annotation.ResponseExcel;
import com.loong.loong.common.log.annotation.SysLog;
import com.loong.loong.hik.entity.Snap;
import com.loong.loong.hik.service.SnapService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 抓拍计划
 *
 * @author jinyiang
 * @date 2023-03-29 17:11:56
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/snap" )
@Api(value = "snap", tags = "抓拍计划管理")
public class SnapController {

    private final  SnapService snapService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param snap 抓拍计划
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('hik_snap_view')" )
    public R getSnapPage(Page page, Snap snap) {

		return R.ok(snapService.page(page,
				Wrappers.query(snap).lambda()
						.like(StringUtils.isNotEmpty(snap.getLikeName()), Snap::getDeviceName,snap.getLikeName())
						.between(StringUtils.isNotEmpty(snap.getStartTime())&&StringUtils.isNotEmpty(snap.getEndTime()),Snap::getCreateTime,snap.getStartTime(),snap.getEndTime())));
    }


    /**
     * 通过id查询抓拍计划
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('hik_snap_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(snapService.getById(id));
    }

    /**
     * 新增抓拍计划
     * @param snap 抓拍计划
     * @return R
     */
    @ApiOperation(value = "新增抓拍计划", notes = "新增抓拍计划")
    @SysLog("新增抓拍计划" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('hik_snap_add')" )
    public R save(@RequestBody Snap snap) {
        return R.ok(snapService.save(snap));
    }

    /**
     * 修改抓拍计划
     * @param snap 抓拍计划
     * @return R
     */
    @ApiOperation(value = "修改抓拍计划", notes = "修改抓拍计划")
    @SysLog("修改抓拍计划" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('hik_snap_edit')" )
    public R updateById(@RequestBody Snap snap) {
        return R.ok(snapService.updateById(snap));
    }

    /**
     * 通过id删除抓拍计划
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除抓拍计划", notes = "通过id删除抓拍计划")
    @SysLog("通过id删除抓拍计划" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('hik_snap_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(snapService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param snap 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('hik_snap_export')" )
    public List<Snap> export(Snap snap) {
        return snapService.list(Wrappers.query(snap));
    }
}
